$(document).ready(function() {
	$("#cbrmb_freight_upload_indexpage").removeClass("sub-menu-link").addClass("selected");
	var numArray = ['0','1','2','3','4','5','6','7','8','9'];
	$("#defButton").click(function() {
		var randomN = getRandomNum();
		$("#merOrderNum").val(randomN);
		var resultArray = [];
		for (var i = 0; i < 20; i++) {
		    var index = parseInt(Math.random()*(numArray.length));
		    resultArray.push(numArray[index]);
		}
		var result = resultArray.join();
		while (result.indexOf(",") != -1) {
			result = result.replace(",","");
		}
//		var randomFreightNum = randomFreightNum(20);
		$("#freightNum").val(result);
		defaultSelectFreightType();
	});
	
	$.formValidator.initConfig({
		validatorgroup : "1",
		formid : "fright_upload_form",
		onerror : function(input1, input2) {
			return false;
		}
	});

	$("#tranCode").formValidator({
		onshow : " "
	}).inputValidator({
		min : 1,
		onerror : "交易码不允许为空！"
	}).functionValidator({
		fun : function(val) {
			if (val != "7025") {
				return "交易码必须为 7025";
			} else {
				return true;
			}
		}
	});
	
	$("#merchantId").formValidator({
		onshow : " "
	}).inputValidator({
		min : 1,
		onerror : "商户ID不允许为空！"
	});
	
	$("#merOrderNum").formValidator({
		onshow : " "
	}).inputValidator({
		min : 1,
		onerror : "订单号不允许为空！"
	});
});

function getRandomNum() {
	window.random_code_base = (([ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
			'K', 'M', 'N' ])[new Date().getMonth()])
			+ '_' + (Math.random() * 10000000000 + '').substring(0, 4);
	window.random_code_index = 0;
	return window.random_code_base + "_" + (window.random_code_index++);
}

function randomFreightNum(len) {
	var numArray = ['0','1','2','3','4','5','6','7','8','9'];
	var resultArray = [];
	for (var i = 0; i < len; i++) {
	    var index = parseInt(Math.random()*(numArray.length));
	    resultArray.push(numArray[index]);
	}
	var result = resultArray.join();
	while (result.indexOf(",") != -1) {
		result = result.replace(",","");
	}
	return result;
}

function defaultSelectFreightType() {
	var select = document.getElementById("freight_type");
	select.options[1].selected = true;
}